package jvm.chapter3;

/**
 * @program: algorithm
 * @description: 新生代MinorGC
 * @author: 陈恩涛
 * @create: 2021-10-27 18:48
 **/
public class ObjectAllocationTest {

    private static final int _1MB = 1024 * 1024;

    /**
     * VM参数：-verbose:gc -Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails -XX:SurvivorRatio=8 -XX:+UseSerialGC
     */
    public static void testAllocation() {
        byte[] allocation01, allocation02, allocation03, allocation04;

        allocation01 = new byte[2 * _1MB];
        allocation02 = new byte[2 * _1MB];
        allocation03 = new byte[2 * _1MB];
        allocation04 = new byte[4 * _1MB];
    }

    /**
     * VM参数： -verbose:gc -Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails -XX:SurvivorRatio=8 -XX:PretenureSizeThreshold=3145728
     * -XX:+UseSerialGC   注意，这里的-XX:PretenureSizeThreshold参数只对Serial和ParNew两款新生代收集器生效
     */
    public static void testPretenureSizeThreshold() {
        byte[] allocation;
        allocation = new byte[4 * _1MB];
    }

    /**
     * VM参数： -verbose:gc -Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails -XX:SurvivorRatio=8 -XX:MaxTenuringThreshold=1
     * -XX:+PrintTenuringDistribution -XX:+UseSerialGC
     */
    public static void testTenuringThreshold() {
        byte[] allocation01, allocation02, allocation03;
        allocation01 = new byte[_1MB / 4];
        allocation02 = new byte[4 * _1MB];
        allocation03 = new byte[4 * _1MB];
        allocation03 = null;
        allocation03 = new byte[4 * _1MB];
    }

    /**
     * VM参数：-verbose:gc -Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails -XX:SurvivorRatio=8
     * -XX:MaxTenuringThreshold=15 -XX:+PrintTenuringDistribution -XX:+UseSerialGC
     */
    public static void testTenuringThreshold2() {
        byte[] allocation01, allocation02, allocation03, allocation04;
        allocation01 = new byte[_1MB / 4];
        allocation02 = new byte[_1MB / 4];
        allocation03 = new byte[4 * _1MB];
        allocation04 = new byte[4 * _1MB];
        allocation04 = null;
        allocation04 = new byte[4 * _1MB];
    }

    /**
     * VM参数：-Xms20M -Xmx20M -Xmn10M -XX:+PrintGCDetails -XX:SurvivorRatio=8 -XX:-HandlePromotionFailure=true -XX:+UseSerialGC
     */
    public static void testHandlePromotion() {
        byte[] allocation01, allocation02, allocation03, allocation04, allocation05, allocation06, allocation07;
        allocation01 = new byte[2 * _1MB];
        allocation02 = new byte[2 * _1MB];
        allocation03 = new byte[2 * _1MB];
        allocation01 = null;
        allocation04 = new byte[2 * _1MB];
        allocation05 = new byte[2 * _1MB];
        allocation06 = new byte[2 * _1MB];
        allocation04 = null;
        allocation05 = null;
        allocation06 = null;
        allocation07 = new byte[2 * _1MB];
    }

    public static void main(String[] args) {
//        testAllocation();
//        testPretenureSizeThreshold();
//        testTenuringThreshold();
//        testTenuringThreshold2();
        testHandlePromotion();
    }
}
